# coding=utf-8

import datetime
import subprocess
# from .utils.screen_snapshot import get_snapshot
# from utils.screen_snapshot import get_snapshot
import sys
import time

sys.path.append(r'E:\codepiece\junp-4-double-twelve')
from utils.screen_snapshot import get_snapshot

JUMP_POSITION = (593, 1112)
RESULT_CLOSE_POSITION = (720, 560)
JUMP_ANIMATION_DURATION_IN_SECONDS = 20
RESULT_ANIMATION_DURATION_IN_SECONDS = 1
SCREEN_CAP_PATH = 'E:\\codepiece\\junp-4-double-twelve\\screenshot'


def connect_phone():
    return True
    result = subprocess.check_output("adb devices")
    print(result)
    return " device " in result


def touch_screen(x, y):
    subprocess.Popen("adb shell input touchscreen tap {x} {y}".format(x=x, y=y)).wait()


def back():
    subprocess.Popen("adb shell input keyevent 4").wait()


if __name__ == "__main__":
    if not connect_phone():
        print("Connect phone failed")

    if None:
        start_time = datetime.datetime.now().strftime("%Y-%m-%d_%H_%M_%S")
        get_snapshot("{}\{}-start.png".format(SCREEN_CAP_PATH, start_time))

    for i in range(20):
        print(u"\033[1;35m{:8d} 次浏览\033[0m".format(i + 1))
        # 点击 去浏览
        touch_screen(JUMP_POSITION[0], JUMP_POSITION[1])

        print("等待 {} 浏览倒计时".format(JUMP_ANIMATION_DURATION_IN_SECONDS))
        # 等待起跳动画完结，显示结果
        time.sleep(JUMP_ANIMATION_DURATION_IN_SECONDS)

        print("返回")
        # 关闭结果
        back()

        # 等待起跳画面再次就绪
        print("等待 {} 再次浏览".format(RESULT_ANIMATION_DURATION_IN_SECONDS))
        time.sleep(RESULT_ANIMATION_DURATION_IN_SECONDS)

    # get_snapshot("{}\\{}-end.png".format(SCREEN_CAP_PATH, start_time))
